package com.gzz.orchid.common.core.tuple;


import lombok.Value;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Currency;


/**
 * 汇率
 *
 * @author higmd
 */
@Value
public class ExchangeRate {
    BigDecimal rate;
    Currency from;
    Currency to;
    // 构造函数
    public ExchangeRate(BigDecimal rate, Currency from, Currency to) {
        this.rate = rate;
        this.from = from;
        this.to = to;
    }

    public Money exchange(Money fromMoney) {
        Assert.notNull(fromMoney, "fromMoney can not be null");
        Assert.isTrue(this.from.equals(fromMoney.getCurrency()), "fromMoney's currency must be same as this's from");
        BigDecimal targetAmount = fromMoney.getValue().multiply(rate);
        return new Money(targetAmount, to);
    }
}
